//
//  UITableView+PlaceHolder.m
//  HealthManage
//
//  Created by leqiang222 on 2017/5/9.
//  Copyright © 2017年 kinglian. All rights reserved.
//

#import <objc/runtime.h>

@interface UITableView ()
/** 占位图片 */
@property (nonatomic, strong) UIView *placeHolderView;
/** 占位控件能否滚动 */
@property (nonatomic, assign) BOOL scrollWasEnabled;
@end

@implementation UITableView (PlaceHolder)

#pragma mark - Public
- (void)app_reloadData {
    [self reloadData];
    
    [self app_checkEmpty];
}

#pragma mark - 绑定
- (BOOL)scrollWasEnabled {
    NSNumber *scrollWasEnabledObject = objc_getAssociatedObject(self, @selector(scrollWasEnabled));
    return [scrollWasEnabledObject boolValue];
}

- (void)setScrollWasEnabled:(BOOL)scrollWasEnabled {
    NSNumber *scrollWasEnabledObject = [NSNumber numberWithBool:scrollWasEnabled];
    objc_setAssociatedObject(self, @selector(scrollWasEnabled), scrollWasEnabledObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)placeHolderView {
    return objc_getAssociatedObject(self, @selector(placeHolderView));
}

- (void)setPlaceHolderView:(UIView *)placeHolderView {
    objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - Private
- (void)app_checkEmpty {
    BOOL isEmpty = YES;
    
    id<UITableViewDataSource> src = self.dataSource;
    NSInteger sections = 1;
    if ([src respondsToSelector: @selector(numberOfSectionsInTableView:)]) {
        sections = [src numberOfSectionsInTableView:self];
    }
    for (int i = 0; i<sections; ++i) {
        NSInteger rows = [src tableView:self numberOfRowsInSection:i];
        if (rows) {
            isEmpty = NO;
        }
        
    }
    if (!isEmpty != !self.placeHolderView) {
        if (isEmpty) {
            self.scrollWasEnabled = self.scrollEnabled;
            
            BOOL scrollEnabled = NO;
            if ([self respondsToSelector:@selector(isPlaceHolderViewEnableScroll)]) {
                scrollEnabled = (BOOL)[self performSelector:@selector(isPlaceHolderViewEnableScroll)];
                
            }else if ([self.delegate respondsToSelector:@selector(isPlaceHolderViewEnableScroll)]) {
                scrollEnabled = (BOOL)[self.delegate performSelector:@selector(isPlaceHolderViewEnableScroll)];
            }
            
            self.scrollEnabled = scrollEnabled;
            
            if ([self respondsToSelector:@selector(makePlaceHolderView)]) {
                self.placeHolderView = [self performSelector:@selector(makePlaceHolderView)];
                
            } else if ( [self.delegate respondsToSelector:@selector(makePlaceHolderView)]) {
                self.placeHolderView = [self.delegate performSelector:@selector(makePlaceHolderView)];
                
            } else {
                NSString *selectorName = NSStringFromSelector(_cmd);
                NSString *reason = [NSString stringWithFormat:@"如果想使用 %@, 必须先实现makePlaceHolderView 这个方法", selectorName];
                @throw [NSException exceptionWithName:NSGenericException reason:reason userInfo:nil];
            }
            self.placeHolderView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            [self addSubview:self.placeHolderView];
            
        } else {
            self.scrollEnabled = self.scrollWasEnabled;
            [self.placeHolderView removeFromSuperview];
            self.placeHolderView = nil;
        }
        
    } else if (isEmpty) {
        [self.placeHolderView removeFromSuperview];
        [self addSubview:self.placeHolderView];
    }
}

@end
